package com.ws.app.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ws.app.common.ControllerSupport;
import com.ws.app.service.ExpenseService;
import com.ws.base.common.util.WSUtil;
import com.ws.base.db.common.order.Order;
import com.ws.base.db.common.page.Page;

@Controller
public class CostController extends ControllerSupport {

    @Resource(name = "ws_app_expenseService")
    private ExpenseService expenseService;

    @RequestMapping("/editCost")
    public void editCost(HttpServletRequest request,
            HttpServletResponse response) {
        String id = request.getParameter("id");
        String price = request.getParameter("price");
        String description = request.getParameter("description");
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("id", id);
        condition.put("price", price);
        condition.put("remark", description);
        expenseService.updateExpense(condition);

        // TODO editCost
        Map<String, String> rsMap = new HashMap<String, String>();
        rsMap.put("success", "true");
        rsMap.put("msg", "xixi");
        this.outputJSON(rsMap, response);
    }

    @RequestMapping("/queryCost")
    public void queryCost(HttpServletRequest request,
            HttpServletResponse response) {
        // TODO
        String currentPage = request.getParameter("page");
        // String sort = request.getParameter("sort");
        // String start = request.getParameter("start");
        String limit = request.getParameter("limit");
        Page page = new Page(Integer.valueOf(currentPage),
                Integer.valueOf(limit));

        Order order = new Order(true, "createdate");
        List<Map<String, Object>> list = expenseService.queryExpense(null,
                page, order);
        int total = page.getTotalRows();

        for (Map<String, Object> map : list) {
            map.put("date",
                    WSUtil.sDateFmt().format((Date) map.get("createdate")));
        }

        Map<String, Object> rsMap = new HashMap<String, Object>();
        rsMap.put("data", list);
        rsMap.put("pageTotal", total);
        this.outputJSON(rsMap, response);
    }

    public ExpenseService getExpenseService() {
        return expenseService;
    }

    public void setExpenseService(ExpenseService expenseService) {
        this.expenseService = expenseService;
    }

}
